﻿using NLog;
using NLog.Config;
using NLog.Targets;

namespace autoctrl.utils {
  public static class LogHelper
  {
    public static void Configure()
    {
      var config = new LoggingConfiguration();

      // 创建目标
      var fileTarget = new FileTarget("file")
      {
        FileName = "${basedir}/Logs/${shortdate}.log",
        Layout = "${longdate} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}",
        ArchiveFileName = "${basedir}/Logs/archive-{#}.log",
        ArchiveEvery = FileArchivePeriod.Day,
        ArchiveNumbering = ArchiveNumberingMode.Rolling,
        MaxArchiveFiles = 7,
        ConcurrentWrites = true
      };

      var consoleTarget = new ConsoleTarget("console")
      {
        Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}"
      };

      config.AddTarget(fileTarget);
      config.AddTarget(consoleTarget);

      config.AddRule(LogLevel.Debug, LogLevel.Fatal, fileTarget);
      config.AddRule(LogLevel.Info, LogLevel.Fatal, consoleTarget);

      LogManager.Configuration = config;
    }
  }

}
